package com.study.demo01.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * <a href="https://img-blog.csdnimg.cn/758bdcfea0b14045acaf859a26ad3f5e.png">发送确认</a>
 */
@Configuration
public class ConfirmConfig {

    //交换机
    public static final String confirm_exchange_name = "confirm_exchange";

    public static final String confirm_queue_name = "confirm_queue";
    public static final String confirm_queue_name_b = "confirm_queue-b";
    public static final String confirm_routing_key = "key1";
    public static final String confirm_routing_key2 = "key2";

    @Bean(confirm_exchange_name)
    public DirectExchange confirmExchange() {
        //指定备份交换机
        return ExchangeBuilder.directExchange(confirm_exchange_name).durable(true)
                //.withArgument("alternate-e", "")这行代码可以用下面的代替
                .alternate(back_exchange)
                .build();

        //return new DirectExchange(confirm_exchange_name);
    }

    @Bean(confirm_queue_name)
    public Queue confirmQueue() {
        return QueueBuilder.durable(confirm_queue_name).build();
    }
    @Bean(confirm_queue_name_b)
    public Queue confirmQueueB() {
        return QueueBuilder.durable(confirm_queue_name_b).build();
    }
    @Bean
    public Binding queueBinding(@Qualifier(confirm_exchange_name) DirectExchange exchange, @Qualifier(confirm_queue_name) Queue queue) {
        return BindingBuilder.bind(queue).to(exchange).with(confirm_routing_key);
    }
    @Bean
    public Binding queueBBinding(@Qualifier(confirm_exchange_name) DirectExchange exchange, @Qualifier(confirm_queue_name_b) Queue queue) {
        return BindingBuilder.bind(queue).to(exchange).with(confirm_routing_key2);
    }

    //![备份交换机](https://img-blog.csdnimg.cn/0d0b727799174fc3b4ae7d0ed749c669.png)


    //TODO 备份交换机 和 回退 同时配置时, 以备份交换机为准,备份交换机的优先级高
    /**
     * 备份交换机
     */
    public static final String back_exchange = "backup_exchange";

    public static final String backu_queue = "backup_queue";
    public static final String waring_queue = "waring_queue";

    @Bean(back_exchange)
    public FanoutExchange backExchange() {
        return new FanoutExchange(back_exchange);

    }

    /**
     * 备份队列
     */
    @Bean(backu_queue)
    public Queue backQueue() {
        return QueueBuilder.durable(backu_queue).build();
    }

    /**
     * 报警交换机
     */
    @Bean(waring_queue)
    public Queue waringQueue() {
        return QueueBuilder.durable(waring_queue).build();
    }

    @Bean
    public Binding backqueueBinding(@Qualifier(back_exchange) FanoutExchange exchange, @Qualifier(backu_queue) Queue queue) {
        return BindingBuilder.bind(queue).to(exchange);
    }

    @Bean
    public Binding waringqueueBinding(@Qualifier(back_exchange) FanoutExchange exchange, @Qualifier(waring_queue) Queue queue) {
        return BindingBuilder.bind(queue).to(exchange);
    }
}
